extends "res://BaseEnemy.gd"

# 精英森林树妻特有属性
@export var root_attack_cooldown = 7.0  # 根系攻击冷却时间
@export var root_attack_damage = 15  # 根系攻击伤害
@export var root_attack_duration = 3.0  # 根系束缚持续时间
@export var healing_aura_radius = 120.0  # 治疗光环半径
@export var healing_amount = 5  # 每秒治疗量

var root_attack_timer = 0.0
var can_root_attack = true
var healing_timer = 0.0

func _ready():
	# 调用父类的_ready方法
	super._ready()
	
	# 设置精英森林树妻特有属性
	max_health = 150
	current_health = max_health
	movement_speed = 50.0
	damage = 12
	experience_value = 30
	is_elite = true
	
	# 更新生命值条
	if health_bar:
		health_bar.max_value = max_health
		health_bar.value = current_health
	
	# 设置外观
	if sprite:
		sprite.modulate = Color(0.7, 1.2, 0.7)  # 精英树妻颜色

func _physics_process(delta):
	# 调用父类的_physics_process方法
	super._physics_process(delta)
	
	# 处理根系攻击冷却
	if not can_root_attack:
		root_attack_timer += delta
		if root_attack_timer >= root_attack_cooldown:
			can_root_attack = true
			root_attack_timer = 0.0
	
	# 治疗光环
	healing_timer += delta
	if healing_timer >= 1.0:  # 每秒治疗一次
		healing_timer = 0.0
		heal_nearby_allies()
	
	# 如果可以使用根系攻击且玩家存在，尝试根系攻击
	if can_root_attack and target_player and is_instance_valid(target_player):
		var distance = global_position.distance_to(target_player.global_position)
		if distance <= 150:  # 在适当距离时使用根系攻击
			root_attack()

func root_attack():
	if animation_player and animation_player.has_animation("root_attack"):
		animation_player.play("root_attack")
		
		# 创建根系效果
		var roots = preload("res://RootEffect.tscn").instantiate()
		get_tree().get_root().add_child(roots)
		
		# 设置根系位置（玩家位置）
		if target_player and is_instance_valid(target_player):
			roots.global_position = target_player.global_position
			
			# 对玩家造成伤害
			target_player.take_damage(root_attack_damage)
			
			# 束缚效果
			if target_player.has_method("apply_root_effect"):
				target_player.apply_root_effect(root_attack_duration)
			else:
				# 如果玩家没有束缚方法，使用减速效果代替
				if target_player.has_method("apply_slow_effect"):
					target_player.apply_slow_effect(0.2, root_attack_duration)  # 强力减速
		
		# 重置根系攻击冷却
		can_root_attack = false
		root_attack_timer = 0.0
		
		print("精英树妻使用根系攻击!")

func heal_nearby_allies():
	# 获取附近的敌人
	var enemies = get_tree().get_nodes_in_group("enemies")
	
	for enemy in enemies:
		if enemy != self and is_instance_valid(enemy) and enemy.has_method("heal"):
			var distance = global_position.distance_to(enemy.global_position)
			if distance <= healing_aura_radius:
				# 治疗盟友
				enemy.heal(healing_amount)
				
				# 创建治疗效果
				var heal_effect = preload("res://HealEffect.tscn").instantiate()
				get_tree().get_root().add_child(heal_effect)
				heal_effect.global_position = enemy.global_position
	
	# 自身也恢复生命值
	heal(healing_amount)

# 添加治疗方法
func heal(amount):
	if current_health < max_health:
		current_health = min(current_health + amount, max_health)
		
		# 更新生命值条
		if health_bar:
			health_bar.value = current_health
			health_bar.visible = true
			
			# 创建计时器在一段时间后隐藏生命值条
			var timer = get_tree().create_timer(3.0)
			timer.connect("timeout", _on_health_bar_timer_timeout)
		
		# 创建治疗效果
		var heal_effect = preload("res://HealEffect.tscn").instantiate()
		get_tree().get_root().add_child(heal_effect)
		heal_effect.global_position = global_position
